/*------------------------------------------------------------------------------------------------------------------------------------------
--		SOURCE FILE:	winMain.cpp		- 
--
--		PROGRAM:		
--
--		FUNCTIONS:		WinMain	
--						WndProc
--						
--
--		DATE:			October 24, 2011	
--
--		REVISIONS:		1.0
--
--		DESIGNER:		Mike Zobac
--
--		PROGRAMMER		Mike Zobac/Chris Sim
--
--		NOTES:			
--------------------------------------------------------------------------------------------------------------------------------------------*/

#include "includes.h"
#include "Connect.h"
#include "Command.h"


TTYINFO		info;
PTTYINFO	dlg_pinfo = NULL;

/*-------------------------------------------------------------------------------------------------------------------------------------------
--		FUNCTION:		WinMain			- Windows recognizes this function as the entry point for the program.  It initializes the window, 
											and initiates a loop which retrieves and dispatches messages.							
--
--		DATE:			October 24, 2011
--
--		DESIGNER:		Mike Zobac
--
--		PROGRAMMER:		Mike Zobac
--
--		INTERFACE:		int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
--
--		RETURNS:		int
--
--		NOTES:			
--------------------------------------------------------------------------------------------------------------------------------------------*/

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
                    
{
     MSG            msg;
     WNDCLASS       wndclass;
     int            xWindow = 850, yWindow = 480;
     static TCHAR   szAppName[] = TEXT ("Terminal") ;
     HWND		    hwnd, dlghwnd;
	 
     
     wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
     wndclass.lpfnWndProc   = WndProc ;
     wndclass.cbClsExtra    = 0 ;
     wndclass.cbWndExtra    = 0 ;
     wndclass.hInstance     = hInstance ;
     wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
     wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
     wndclass.lpszMenuName  = TEXT("MYMENU");
     wndclass.lpszClassName = szAppName ;
     
     if (!RegisterClass (&wndclass))
     {
          MessageBox (NULL, TEXT ("This program requires Windows NT!"), 
                      szAppName, MB_ICONERROR) ;
          return 0 ;
     }
     hwnd = CreateWindow (szAppName, TEXT ("Wireless Modem Protocol Driver"),
                          WS_OVERLAPPEDWINDOW | WS_VSCROLL,
                          CW_USEDEFAULT, CW_USEDEFAULT,
                          xWindow, yWindow,
                          NULL, info.hMenu, hInstance, NULL) ;	 

     ShowWindow (hwnd, iCmdShow) ;
     UpdateWindow (hwnd) ;

     while (GetMessage (&msg, NULL, 0, 0))
     {
        TranslateMessage (&msg) ;
        DispatchMessage  (&msg) ;
     }
     
     return msg.wParam ;
}


/*-------------------------------------------------------------------------------------------------------------------------------------------
--		FUNCTION:		WndProc			- Processes the messages for the program window, or passes them on the the default WndProc.
--
--		DATE:			October 24, 2011
--
--		DESIGNER:		Mike Zobac
--
--		PROGRAMMER:		Mike Zobac/Chris Sim
--
--		INTERFACE:		LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
--
--		RETURNS:		LRESULT 
--
--		NOTES:			
--------------------------------------------------------------------------------------------------------------------------------------------*/

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	static TTYINFO info;

     switch (message)
     {
     case WM_INPUTLANGCHANGE:
          wmInputLang(wParam);

     case WM_CREATE:
          wmCreate(hwnd, &info, (LPCREATESTRUCT)lParam);              

	 case WM_COMMAND:
		  callMenu(hwnd, &info, wParam); 
		  return 0;
	 
	 case WM_SIZE:
          wmSize(hwnd, message, lParam, &info);
          return 0 ;
                    
     case WM_VSCROLL:
          wmVscroll(hwnd, wParam, &info) ;
		  return 0;
            
     case WM_PAINT:
          wmPaint(hwnd, &info);
		  return 0;
          
      case WM_DESTROY:
          info.bReading = false;
          CloseHandle (info.readThread);
		  CloseHandle (info.protocolThread);
          CloseHandle (info.hPort);
		  fclose(info.inputFile);
          PostQuitMessage (0) ;
          return 0 ;
     }
     return DefWindowProc (hwnd, message, wParam, lParam) ;
}


/*-------------------------------------------------------------------------------------------------------------------------------------------
--		FUNCTION:		DlgProc			
--
--		DATE:			Dec 5, 2011
--
--		DESIGNER:		
--
--		PROGRAMMER:		
--
--		INTERFACE:		LRESULT CALLBACK DlgProc (HWND, UINT, WPARAM, LPARAM);
--
--		RETURNS:		LRESULT
--
--		NOTES:			
--------------------------------------------------------------------------------------------------------------------------------------------*/

LRESULT CALLBACK DlgProc(HWND hWndDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
{
	HDC hdc;
	char PacketsTransfered[50], PacketsReceived[50], AcksSent[50], AcksReceived[50], EnqsSent[50], EnqsReceived[50], PER[50],
		 CorruptPacketsReceived[50], CorruptPacketsSent[50], DuplicatePacketsReceived[50], DuplicatePacketsSent[50] = {0};
	switch(Msg)
	{
	case WM_INITDIALOG:
		return TRUE;

	case WM_CREATE:
		dlg_pinfo = (PTTYINFO)wParam;

	case WM_PAINT:
		if(dlg_pinfo != NULL)
		{
			hdc = GetDC(hWndDlg);
			//info.num_PER = info.num_CorruptPacketsReceived / (info.num_CorruptPacketsReceived + info.num_DuplicatePacketsReceived + info.num_PacketsReceived);
			sprintf(PacketsTransfered, "Packets Transfered: %d", dlg_pinfo->num_PacketsTransfered);
			TextOut(hdc, 10, 10, PacketsTransfered, strlen(PacketsTransfered));
			sprintf(PacketsReceived, "Packets Received: %d", dlg_pinfo->num_PacketsReceived);
			TextOut(hdc, 10, 30, PacketsReceived, strlen(PacketsReceived));
			sprintf(AcksSent, "Acks Sent: %d", dlg_pinfo->num_AcksSent);
			TextOut(hdc, 10, 50, AcksSent, strlen(AcksSent));
			sprintf(AcksReceived, "Acks Received: %d", dlg_pinfo->num_AcksReceived);
			TextOut(hdc, 10, 70, AcksReceived, strlen(AcksReceived));
			sprintf(EnqsSent, "Enqs Sent: %d", dlg_pinfo->num_EnqsSent);
			TextOut(hdc, 10, 90, EnqsSent, strlen(EnqsSent));
			sprintf(EnqsReceived, "Enqs Received: %d", dlg_pinfo->num_EnqsReceived);
			TextOut(hdc, 10, 110, EnqsReceived, strlen(EnqsReceived));
			sprintf(PER, "PER: %d", dlg_pinfo->num_PER);
			TextOut(hdc, 10, 130, PER, strlen(PER));
			sprintf(CorruptPacketsReceived, "Corrupt Packets Received: %d", dlg_pinfo->num_CorruptPacketsReceived);
			TextOut(hdc, 10, 150, CorruptPacketsReceived, strlen(CorruptPacketsReceived));
			sprintf(CorruptPacketsSent, "Corrupt Packets Sent: %d", dlg_pinfo->num_CorruptPacketsSent);
			TextOut(hdc, 10, 170, CorruptPacketsSent, strlen(CorruptPacketsSent));
			sprintf(DuplicatePacketsReceived, "Duplicate Packets Received: %d", dlg_pinfo->num_DuplicatePacketsReceived);
			TextOut(hdc, 10, 190, DuplicatePacketsReceived, strlen(DuplicatePacketsReceived));
			sprintf(DuplicatePacketsSent, "Duplicate PacketsSent: %d", dlg_pinfo->num_DuplicatePacketsSent);
			TextOut(hdc, 10, 210, DuplicatePacketsSent, strlen(DuplicatePacketsSent));
			ReleaseDC(hWndDlg, hdc);
		}
    }

	return FALSE;
}